package com.china_goldcard.igas.http.base;

import com.china_goldcard.igas.http.api.CommonAPI;
import com.china_goldcard.igas.http.api.UserAPI;
import com.china_goldcard.igas.http.model.common.resp.VersionGetResp;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 请求方法对象
 */
public class APIHelper {

    // 基础url
    public static final String BASE_URL = "http://appserver-app-qa.china-goldcard.com/";

    // 超时时间
    private static final int DEFAULT_TIMEOUT = 5;

    // Retrofit对象
    private Retrofit retrofit;

    // 用户API
    private UserAPI userAPI;

    // 公共API
    private CommonAPI commonAPI;

    // 实体类
    private static APIHelper instance;

    /**
     * 私有的构造方法
     */
    private APIHelper() {
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        // 日志打印拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 请求头拦截器
        HeaderInterceptor headerInterceptor = new HeaderInterceptor();
        // 网络是否可用拦截器
        NetAvailableInterceptor netAvailableInterceptor = new NetAvailableInterceptor();
        httpClientBuilder.addInterceptor(loggingInterceptor);
        httpClientBuilder.addInterceptor(headerInterceptor);
        httpClientBuilder.addInterceptor(netAvailableInterceptor);
        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        userAPI = getAPI(UserAPI.class);
        commonAPI = getAPI(CommonAPI.class);
    }

    /**
     * 获取单例
     * @return
     */
    public static APIHelper getInstance(){
        if (instance ==  null){
            instance = new APIHelper();
        }
        return instance;
    }

    /**
     * 获取对应的API
     * @param api
     * @param <T>
     * @return
     */
    public <T> T getAPI(Class<T> api){
        return retrofit.create(api);
    }


    /**
     * 获取版本号
     * @param handler
     */
    public void getVersion(ReqHandler<VersionGetResp> handler){
        this.toSubscribe(commonAPI.getVersion(),new RespSubscriber(handler));
    }

    /**
     * 转换成subscribe
     * @param o
     * @param s
     * @param <T>
     */
    private <T> void toSubscribe(Observable<T> o, Subscriber<T> s){
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }

}
